package com.whimscrew.store.service;

import com.whimscrew.store.model.StoreChangeDTO;
import com.whimscrew.store.model.StoreDTO;
import com.whimscrew.store.stream.StoreChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.UUID;

//启用消息代理
@EnableBinding(StoreChannel.class)
@Service
public class StroeCacheService   {

    private final static Logger logger = LoggerFactory.getLogger(StroeCacheService.class);

    @Autowired
    private StoreChannel storeChannel;

    //发布缓存的变更消息
    public void publishChange(String action,String areaCode, Set<StoreDTO> cacheStores){
        logger.debug("Sending  message for Store-service action: {}", action );
        StoreChangeDTO change =  new StoreChangeDTO();
        //tokenid实际用于消息追踪，应结合实际情况的项目进行设置
        change.setStoreList(cacheStores).setAction(action)
                .setTokenId(UUID.randomUUID().toString()).setAreaCode(areaCode);
        //发送消息
        storeChannel.storeChangeChannel().send(MessageBuilder.withPayload(change).build());
    }

}
